<html>
<head>
<script type="text/javascript" src="lib/3rdparty/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="lib/3rdparty/jquery.effects.core.min.js"></script>
<script type="text/javascript" src="lib/csmm/TwitterConnector.js"></script>
<script type="text/javascript" src="lib/csmm/BuzzConnector.js"></script>

<script type="text/javascript" src="engine.js"></script>
<script type="text/javascript" src="probe_manager.js"></script>
<script type="text/javascript" src="probe.js"></script>

<script type="text/javascript" src="queryURL.js"></script>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">$(pluginInit);</script>
</head>
<body>
<script type="text/javascript">
const CORRECTION_FACTOR = 0.8;
//const ALERT_RATIO = 3;

var baseURL = 'http://search.twitter.com/search.json?';
var probeManager = new ProbeManager();
var twitterConnector = new TwitterConnector();
var buzzConnector = new BuzzConnector();

var localPort;
const extension = chrome.extension;
extension.onConnect.addListener(function(port) {
	console.assert(port.name == "social-media-monitoring");
	localPort = port;
	port.onMessage.addListener(function(msg) {
		console.log('BackgroundPage, onMessage:' + msg);
		  
		if (msg.probeTagChanged) {
			var probeTag = msg.probeTagChanged;
			loadActivities(probeTag, port);
		}
		if (msg.requestGeolocation) {
		  	requestGeoLocation();
		}
	});
});




function requestGeoLocation(){
	if (navigator.geolocation) {
		console.log('Requesting HTML5 Geolocation...');
	  	navigator.geolocation.getCurrentPosition(locationDetected, locationError);
	} else {
		locationError('Geo location not supported! Geolocation is currently only supported in the dev channels Try start chrome with arguments: --enable geolocation');
	}
}

function locationDetected(position) {
     var latitude = position.coords.latitude;
     var longitude = position.coords.longitude;
     localPort.postMessage({onLocationResult:{latitude:position.coords.latitude,
                                longitude:position.coords.longitude}});   
}

function locationError(msg) {
var status = typeof msg == 'string' ? msg : "failed";
console.log('Error getting HTML5 Geolocation:' + status);
console.log('Now trying to get IPLocation...');
var ipLocation = google.loader.ClientLocation;
if(ipLocation !== null) {
     console.log('Success getting IP Location');
     localPort.postMessage({onLocationResult:{latitude:ipLocation.latitude,longitude:ipLocation.longitude}});
}
else {
     console.log('Error getting IP Location, hello Mountain View');
}
}
function getProbeManager() {
	return probeManager;
}

</script>
</body>
</html>